/**
 * WebSocket Vuex模块
 * 用于管理WebSocket连接状态和消息
 */

const state = {
  connectionStatus: 'closed', // 'connecting', 'open', 'closed', 'error'
  lastMessageTime: null,
  messages: [],
  messageLimit: 100, // 限制存储的消息数量，防止内存泄漏
  notifications: []
}

const mutations = {
  SET_CONNECTION_STATUS(state, status) {
    state.connectionStatus = status
  },
  SET_LAST_MESSAGE_TIME(state, time) {
    state.lastMessageTime = time
  },
  ADD_MESSAGE(state, message) {
    // 添加新消息到消息列表
    state.messages.push({
      ...message,
      timestamp: new Date().getTime(),
      id: `msg_${new Date().getTime()}_${Math.random().toString(36).substr(2, 9)}`
    })
    
    // 如果消息数量超过限制，移除最早的消息
    if (state.messages.length > state.messageLimit) {
      state.messages.shift()
    }
  },
  CLEAR_MESSAGES(state) {
    state.messages = []
  },
  ADD_NOTIFICATION(state, notification) {
    state.notifications.push({
      ...notification,
      timestamp: new Date().getTime(),
      id: `notif_${new Date().getTime()}_${Math.random().toString(36).substr(2, 9)}`
    })
    
    // 限制通知数量
    if (state.notifications.length > 50) {
      state.notifications.shift()
    }
  },
  MARK_NOTIFICATION_READ(state, notificationId) {
    const notification = state.notifications.find(n => n.id === notificationId)
    if (notification) {
      notification.read = true
    }
  },
  CLEAR_NOTIFICATIONS(state) {
    state.notifications = []
  }
}

const actions = {
  /**
   * 添加一条系统通知
   * @param {Object} context Vuex上下文
   * @param {Object} notification 通知对象
   */
  addNotification({ commit }, notification) {
    commit('ADD_NOTIFICATION', {
      ...notification,
      read: false
    })
  },
  
  /**
   * 标记通知为已读
   * @param {Object} context Vuex上下文
   * @param {string} notificationId 通知ID
   */
  markNotificationRead({ commit }, notificationId) {
    commit('MARK_NOTIFICATION_READ', notificationId)
  },
  
  /**
   * 清空所有通知
   * @param {Object} context Vuex上下文
   */
  clearNotifications({ commit }) {
    commit('CLEAR_NOTIFICATIONS')
  },
  
  /**
   * 清空所有消息
   * @param {Object} context Vuex上下文
   */
  clearMessages({ commit }) {
    commit('CLEAR_MESSAGES')
  }
}

const getters = {
  /**
   * 获取WebSocket连接状态
   * @param {Object} state 模块状态
   * @returns {string} 连接状态
   */
  connectionStatus: state => state.connectionStatus,
  
  /**
   * 检查WebSocket是否已连接
   * @param {Object} state 模块状态
   * @returns {boolean} 是否已连接
   */
  isConnected: state => state.connectionStatus === 'open',
  
  /**
   * 获取最近的消息
   * @param {Object} state 模块状态
   * @param {number} limit 消息数量限制
   * @returns {Array} 消息列表
   */
  recentMessages: state => (limit = 10) => {
    return state.messages.slice(-limit).reverse()
  },
  
  /**
   * 获取未读通知数量
   * @param {Object} state 模块状态
   * @returns {number} 未读通知数量
   */
  unreadNotificationCount: state => {
    return state.notifications.filter(n => !n.read).length
  },
  
  /**
   * 获取所有通知
   * @param {Object} state 模块状态
   * @returns {Array} 通知列表
   */
  notifications: state => {
    return state.notifications.slice().reverse()
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
} 